package com.yjkf.zh.admin.controller;

import com.yjkf.zh.admin.aspect.LogAnnotation;
import com.yjkf.zh.admin.dao.SysChannelMapper;
import com.yjkf.zh.admin.dao.SysPointUnitMapper;
import com.yjkf.zh.admin.dao.SysUserDeviceMapper;
import com.yjkf.zh.admin.model.SysChannel;
import com.yjkf.zh.admin.model.SysDevicePointUnit;
import com.yjkf.zh.admin.model.SysUserDevice;
import com.yjkf.zh.admin.service.SysPointUnitService;
import com.yjkf.zh.core.http.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pointUnit")
public class SysPointUnitController {

    @Autowired
    private SysPointUnitMapper sysPointUnitMapper;

    @Autowired
    private SysPointUnitService sysPointUnitService;

    @Autowired
    private SysUserDeviceMapper sysUserDeviceMapper;

    @Autowired
    private SysChannelMapper sysChannelMapper;

    @GetMapping("/findAll")
//    @LogAnnotation(content = "进入设备定义界面")
    public HttpResult findAllById(@RequestParam Long id, HttpServletRequest request){

            if(id==null||id==0){
                Long userId = (Long)request.getSession().getAttribute("userId");
                List<SysUserDevice> sysUserDevices = sysUserDeviceMapper.findByUserId3(userId);
                if(sysUserDevices.isEmpty()){
                    return HttpResult.error("未授权任何设备");
                }
                SysUserDevice sysUserDevice = sysUserDevices.get(0);
                id=sysUserDevice.getId();
            }

        return HttpResult.ok(sysPointUnitService.findAllById(id,request));
    }

    @GetMapping("/findChannelAll")
    public HttpResult findChannelAll(){
        return HttpResult.ok(sysPointUnitMapper.findChannelAll());
    }


    @PostMapping("/save")
    @LogAnnotation(content = "保存设备功能定义操作")
    public HttpResult save(@RequestBody SysDevicePointUnit sysDevicePointUnit,HttpServletRequest request){
        List<SysDevicePointUnit> sysDevicePointUnits = sysPointUnitService.findAllById(sysDevicePointUnit.getUserDeviceId(),request);
        for(SysDevicePointUnit sysDevicePointUnit1:sysDevicePointUnits){
            if(sysDevicePointUnit.getPointName().equals(sysDevicePointUnit1.getPointName())){

                    return HttpResult.error("通道名已存在!");

            }

        }
        return HttpResult.ok(sysPointUnitService.insertPointUnit(sysDevicePointUnit));
    }

    @PostMapping("/update")
    @LogAnnotation(content = "更新设备功能定义操作")
    public HttpResult update(@RequestBody SysDevicePointUnit sysDevicePointUnit,HttpServletRequest request){
        List<SysDevicePointUnit> sysDevicePointUnits = sysPointUnitService.findAllById(sysDevicePointUnit.getUserDeviceId(),request);
        SysDevicePointUnit sysDevicePointUnit0=sysPointUnitMapper.findDpu(sysDevicePointUnit.getUserDeviceId(),sysDevicePointUnit.getDataChannelId());
             if(sysDevicePointUnit0==null){
                 SysChannel sysChannel = sysChannelMapper.findChannelById(sysDevicePointUnit.getDataChannelId());
                 if(!sysChannel.getIdentName().equals(sysDevicePointUnit.getPointName())){       //判断通道名有没有改，if改了
                     for (SysDevicePointUnit sysDevicePointUnit1 : sysDevicePointUnits) {
                         if (sysDevicePointUnit.getPointName().equals(sysDevicePointUnit1.getPointName())) {
                             return HttpResult.error("该通道已存在!");
                         }
                     }
                 }
            }
            if(sysDevicePointUnit0!=null) {
                if (!sysDevicePointUnit0.getPointName().equals(sysDevicePointUnit.getPointName())) {       //判断通道名有没有改，if改了
                    for (SysDevicePointUnit sysDevicePointUnit1 : sysDevicePointUnits) {
                        if (sysDevicePointUnit.getPointName().equals(sysDevicePointUnit1.getPointName())) {
                            return HttpResult.error("该通道已存在!");
                        }
                    }
                }
            }
        /*
      if(sysDevicePointUnits.contains(sysDevicePointUnit.getDeviceName())){
            System.out.println("heellp");
            return HttpResult.error("该单位已经存在!");
        }*/
        return HttpResult.ok(sysPointUnitService.updatePointUnit(sysDevicePointUnit));
    }

    @PostMapping("/delete")
    @LogAnnotation(content = "删除设备功能定义")
    public HttpResult delete(@RequestParam Long userDeviceId, @RequestParam Long dataChannelId){
        return HttpResult.ok(sysPointUnitService.deletePointUnit(userDeviceId,dataChannelId));
    }
}
